package com.example.leero.graduation.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leero.graduation.API;
import com.example.leero.graduation.App;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.FileUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

import okhttp3.Call;

public class SettingActivity extends AppCompatActivity {

    private TextView cache;
    private LinearLayout clearCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        cache = (TextView) findViewById(R.id.cache);
        clearCache = (LinearLayout) findViewById(R.id.clear_cache);

        findViewById(R.id.change_password).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, EditPasswordActivity.class);
                startActivity(intent);
            }
        });

        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("设置");
        findViewById(R.id.iv_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.clear_cache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });

        findViewById(R.id.btn_logout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(SettingActivity.this)
                        .setMessage("确认退出当前帐号吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                logout();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });
    }

    private void deleteCache() {
        final File cacheDir = getCacheDir();
        final File externalCacheDir = getExternalCacheDir();

        new Thread(){
            @Override
            public void run() {
                FileUtil.deleteFilesInDir(cacheDir);
                FileUtil.deleteFilesInDir(externalCacheDir);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(SettingActivity.this, "清除成功", Toast.LENGTH_SHORT).show();
                        initCacheView();
                    }
                });
            }
        }.start();

    }

    private void initCacheView() {
        double size = getCacheSize() / 1024.0;
        if (size < 1024) {
            cache.setText(getString(R.string.cache_size_in_kb, size ));
        } else {
            cache.setText(getString(R.string.cache_size_in_mb, size / 1024.0));
        }

        if (getCacheSize() > 0) {
            clearCache.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog();
                }
            });
        }
    }

    private long getCacheSize() {
        File cacheDir = getCacheDir();
        long size1 = FileUtil.getFolderSize(cacheDir);

        File externalCacheDir = getExternalCacheDir();
        long size2 = FileUtil.getFolderSize(externalCacheDir);

        return size2 + size1;
    }

    private void showDialog() {
        new AlertDialog.Builder(SettingActivity.this)
                .setMessage("要清楚缓存吗？")
                .setNegativeButton("取消", null)
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteCache();
                    }
                })
                .show();
    }

    private void logout() {
        OkHttpUtils.get().url(API.LOGOUT).addParams("user_id", App.getLoginUser().id + "")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getBoolean("state")) {
                        App.logout();
                        Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
